package com.example.hw_cacaowithoutinputpicture;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ListView;

public class ChattingActivity extends Activity {

	public static final int REQUEST_CODE_GET_CONTENT = 0;
	ListView listView;
	CheckBox sendCheck;
	EditText messageView;
	Button btn;

	MessageAdapter mAdapter;
	MessageData[] mData=null;
	ArrayList<MessageData> mList = new ArrayList<MessageData>();

	@Override
	public void onCreate(Bundle savedInstanceState)  {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.chatting_layout);
		listView = (ListView) findViewById(R.id.listView);
		sendCheck = (CheckBox) findViewById(R.id.sendCheck);
		messageView = (EditText) findViewById(R.id.messageText);
		btn = (Button) findViewById(R.id.send);
		mAdapter = new MessageAdapter(this, mList);
		listView.setAdapter(mAdapter);

		btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String text = messageView.getText().toString();

				if (text != null && !text.equals("")) {
					mAdapter.add(new MessageData(DateTime.DatePrint(), text,
							sendCheck.isChecked()));
				}
			}

		});
		
		btn = (Button) findViewById(R.id.dateAdd);
		btn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				mAdapter.add(new MessageData(DateTime.DatePrint(), null, null));
			}
		});
		
		btn = (Button)findViewById(R.id.getPicture);
		btn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent i = new Intent(Intent.ACTION_GET_CONTENT);
				i.setType("image/*");
				startActivityForResult(i, REQUEST_CODE_GET_CONTENT);
			}
		});
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		
		if( requestCode == REQUEST_CODE_GET_CONTENT){
			if(resultCode == Activity.RESULT_OK) {
				Uri uri = data.getData();
//				Bitmap bm = data.getParcelableExtra("data");
				mAdapter.add(new MessageData(null, null, null, uri));
			}
		}
		
	}

	private void setData() {
		// TODO Auto-generated method stub
		if (mData != null) {
			for (MessageData str : mData)
				mList.add(str);
		}
	}

}
